let express = require('express');
let router = express.Router();
let settingDao = require('../../model/admin/setting')

//渲染系统设置界面
router.get('/',async function (req,res) {
    let getCompany  = await settingDao.getCompany();  //获取公司信息
    let getBackupStatus = await settingDao.getBackupStatus(); //获取数据库备份默认设置
      await res.render('setting/setting',{getCompany,getBackupStatus});   
})

//公司信息修改

router.post('/company',async function(req,res){
let id = req.body.id;
let name = req.body.name;
let address = req.body.address;
if(!name){
    return await res.json({status:203,msg:"请输入公司名称！"})
}
if(!address){
    return await res.json({status:203,msg:"请输入公司地址！"})
}
if(!id){
    await settingDao.insertCompany(name,address);
}
else{
    await settingDao.updateCompany(id,name,address);
  
}
await res.json({status:200,msg:'success'})
})


//数据库备份设置

router.post('/backup', async function(req,res){
 let backupStatus =req.body.backupStatus;
 let id = req.body.backupId;
if(!backupStatus){
    return res.json({status:203,msg:"页面错误，请重新加载！"})
}
    backupStatus =1;
   if(backupStatus==false){
    backupStatus =0;    
  }
await settingDao.updateBackupSql(id,backupStatus);
await res.json({status:200,msg:'success'})

})

module.exports= router;